/*
 * File Name:               ColorImage.cs
 * 
 * Description:             普通类
 * Author:                  zhangke <kris@zhanng.org>

 * Create Date:             2017/08/03
 */
using System;
using System.Collections.Generic;

namespace UnityEngine.UI
{

    public class ColorImage : BaseMeshEffect
    {
        [Serializable]
        public struct Colors
        {
            public Color leftBottom;
            public Color leftTop;
            public Color rightBottom;
            public Color rightTop;
        }

        public Colors[] colors = new Colors[0];

        List<UIVertex> list;

        public override void ModifyMesh(VertexHelper vh)
        {
            if (!IsActive()) return;

            if (list == null) list = new List<UIVertex>(6 * colors.Length);

            vh.GetUIVertexStream(list);
            var alpha = 1f;
            if (list.Count >= 6)
            {
                var vert = list[0];

                alpha = vert.color.a / 255f;

                var minX = vert.position.x;
                var maxX = minX;
                var minY = vert.position.y;
                var maxY = minY;

                var uv_min_x = vert.uv0.x;
                var uv_max_x = uv_min_x;
                var uv_min_y = vert.uv0.y;
                var uv_max_y = uv_min_y;

                for (int i = 1; i < 6; i++)
                {
                    vert = list[i];
                    if (vert.position.x < minX) minX = vert.position.x;
                    else if (vert.position.x > maxX) maxX = vert.position.x;

                    if (vert.position.y < minY) minY = vert.position.y;
                    else if (vert.position.y > maxY) maxY = vert.position.y;

                    // uv
                    if (vert.uv0.x < uv_min_x) uv_min_x = vert.uv0.x;
                    else if (vert.uv0.x > uv_max_x) uv_max_x = vert.uv0.x;

                    if (vert.uv0.y < uv_min_y) uv_min_y = vert.uv0.y;
                    else if (vert.uv0.y > uv_max_y) uv_max_y = vert.uv0.y;
                }

                list.Clear();

                if (list.Capacity < 6 * colors.Length)
                {
                    list.Capacity = 6 * colors.Length;
                }

                var offsetX = (maxX - minX) / colors.Length;
                var offset_uv_x = (uv_max_x - uv_min_x) / colors.Length;
                for (int i = 0; i < colors.Length; i++)
                {
                    var c = colors[i];

                    var tmpMinX = minX + i * offsetX;
                    var tmpMaxX = tmpMinX + offsetX;

                    var tmp_uv_min_x = uv_min_x + i * offset_uv_x;
                    var tmp_uv_max_x = tmp_uv_min_x + offset_uv_x;

                    var color = c.leftBottom;
                    color.a = alpha;
                    vert = getUIVertex(tmpMinX, minY, tmp_uv_min_x, uv_min_y, color);
                    list.Add(vert);

                    color = c.leftTop;
                    color.a = alpha;
                    vert = getUIVertex(tmpMinX, maxY, tmp_uv_min_x, uv_max_y, color);
                    list.Add(vert);

                    color = c.rightTop;
                    color.a = alpha;
                    vert = getUIVertex(tmpMaxX, maxY, tmp_uv_max_x, uv_max_y, color);
                    list.Add(vert);
                    list.Add(vert);

                    color = c.rightBottom;
                    color.a = alpha;
                    vert = getUIVertex(tmpMaxX, minY, tmp_uv_max_x, uv_min_y, color);
                    list.Add(vert);

                    color = c.leftBottom;
                    color.a = alpha;
                    vert = getUIVertex(tmpMinX, minY, tmp_uv_min_x, uv_min_y, color);
                    list.Add(vert);
                }
            }

            vh.Clear();
            vh.AddUIVertexTriangleStream(list);
        }

        UIVertex getUIVertex(float x, float y, float uv_x, float uv_y, Color color)
        {
            var vert = new UIVertex();
            vert.position.x = x;
            vert.position.y = y;
            vert.uv0.x = uv_x;
            vert.uv0.y = uv_y;
            vert.color = color;
            return vert;
        }

        public void MarkDirty()
        {
            graphic.SetVerticesDirty();
        }
    }
}
